//
//  TJOrderListNewController.m
//  Teleport
//
//  Created by botu　 on 2020/7/24.
//  Copyright © 2020 byx. All rights reserved.
//

#import "TJOrderListNewController.h"
#import "TJOrderListCell.h"
#import "JXCategoryView.h"
#import "TJOrderListScreeningController.h"
#import "TJQuickTradingNewController.h"
@interface TJOrderListNewController ()<TJOrderListCellDelegate,JXCategoryViewDelegate>
@property (strong, nonatomic) JXCategoryTitleView *categoryView;

@property (strong, nonatomic) NSMutableArray <TJOrderListModel *>*datas;

@property (assign, nonatomic) int pageIndex;
//1,btc 2,usdt
@property (copy, nonatomic) NSString *coinName;
//支付方式 不传为全部订单收款方式,多种的话用-拼接(ALIAY,WECHAT,BANKCARD) 如 ALIAY-WECHAT
@property (copy, nonatomic) NSString *payType;
@end
static NSString * const orderListCell = @"orderListCell";
@implementation TJOrderListNewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
#pragma mark ---------------选择器------------------
    NSArray *titles = @[@"BTC", @"USDT"];
    
    JXCategoryIndicatorLineView *indicatorLineView = [[JXCategoryIndicatorLineView alloc] init];
    indicatorLineView.indicatorHeight = 2;
    indicatorLineView.indicatorColor = UIColor.tj_mainColor;
    indicatorLineView.verticalMargin = 5;
    
    self.categoryView.titles = titles;
    self.categoryView.titleFont = UIFont_Px(15);
    self.categoryView.titleColor = UIColor_Hex(0x262626);
    self.categoryView.titleSelectedColor =UIColor.tj_mainColor;
    self.categoryView.selectedAnimationEnabled = NO;
    self.categoryView.delegate = self;
    self.categoryView.titleLabelZoomEnabled = NO;
    self.categoryView.cellSpacing = 15;
    self.categoryView.frame = CGRectMake(0, 0, 122, 40);
    self.categoryView.indicators = @[indicatorLineView];
    self.categoryView.titleColorGradientEnabled = YES;
    [self.view addSubview:self.categoryView];
    
    
    /** 筛选按钮**/
    UIButton *screeningButton = [UIButton buttonWithSuperView:self.view font:UIFont_Px(12) color:UIColor_Hex(0x3781E7) title:@"".localizedString];
    [screeningButton setImage:[UIImage imageNamed:@"screening_icon_new"] forState:UIControlStateNormal];
    [screeningButton setImage:[UIImage imageNamed:@"screening_icon_new"] forState:UIControlStateHighlighted];
    
    [screeningButton addTarget:self action:@selector(didClickScreeningButton) forControlEvents:UIControlEventTouchUpInside];
    
 
    [screeningButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.height.offset(18);
        make.width.offset(15);
        make.right.offset(-16);
        make.centerY.equalTo(self.categoryView.mas_centerY);
    }];
    
    /**订单按钮 **/
    UIButton *orderButton = [UIButton buttonWithSuperView:self.view font:UIFont_Px(12) color:UIColor_Hex(0x3781E7) title:@"".localizedString];
    [orderButton setImage:[UIImage imageNamed:@"orderList_icon_new"] forState:UIControlStateNormal];
    [orderButton setImage:[UIImage imageNamed:@"orderList_icon_new"] forState:UIControlStateHighlighted];
    [orderButton addTarget:self action:@selector(didClickOrderButton) forControlEvents:UIControlEventTouchUpInside];
   
    [orderButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.size.equalTo(screeningButton);
        make.right.equalTo(screeningButton.mas_left).offset(-15);
        //           make.height.equalTo(coinNameButton);
        make.centerY.equalTo(screeningButton.mas_centerY);
    }];
    

    /**占位图偏移 **/
    self.verticalOffset = -30;
    self.tableView.showsVerticalScrollIndicator = NO;
    self.tableView.rowHeight = 138;
    self.tableView.backgroundColor = UIColor.whiteColor;
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    self.tableView.showsHorizontalScrollIndicator = NO;
    self.tableView.showsVerticalScrollIndicator = NO;
    [self.tableView registerClass:TJOrderListCell.self forCellReuseIdentifier:orderListCell];
    [self.tableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.categoryView.mas_bottom);
        make.left.right.offset(0);
        make.height.offset(self.view.tj_height - KNavBarHight - self.categoryView.tj_height);

    }];

    //默认为第一个
    self.coinName = self.categoryView.titles.firstObject;
    //获取委托单
    [self setUpData];
    
    
    
}
- (void)setUpData{
    [self loadNewData];

    self.tableView.mj_header = self.refreshHeader;
    self.tableView.mj_footer = self.refreshFooter;
 
}

#pragma mark ---------------按钮点击------------------
/**订单按钮点击 **/
- (void)didClickOrderButton {
    [self.navigationController pushViewControllerWithName:@"TJOrderRecardListController" title:@"订单记录".localizedString animated:YES];
}

/**筛选按钮点击 **/
- (void)didClickScreeningButton {
    TJOrderListScreeningController *orderListScreeningController = [[TJOrderListScreeningController alloc] init];
    [orderListScreeningController setSelectComplete:^(NSArray<TJOrderListScreeningModel *> * _Nonnull models) {
        
        TJOrderListScreeningModel *payTypeModel = models.firstObject;
        
        self.payType = payTypeModel.identifier;
        
        //获取委托单
        [self loadNewData];
    }];
    
    [self presentViewController:orderListScreeningController animated:NO completion:nil];
}

#pragma mark -----------------tableViewdegegatel----------------
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.datas.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    TJOrderListCell *cell = [tableView dequeueReusableCellWithIdentifier:orderListCell forIndexPath:indexPath];
    cell.backgroundColor = [UIColor whiteColor];
    cell.model = self.datas[indexPath.row];
    cell.delegate = self;
    return cell;
}
#pragma mark ---------------TJOrderListCellDelegate------------------
- (void)tapSellLabelWithCell:(TJOrderListCell *)cell {
    
    //实名认证
    //跟新用户信息
    weakify(self);
    [TJNetworking.shareNetworking updateUserInfoAndLoading:YES success:^(TJUserInfo * _Nonnull userInfo) {
        if (![userInfo.cardState isEqualToString:@"PASS"]) {
            [weakself alertWithTitle:@"请先实名认证".localizedString message:nil leftButtonName:@"确定".localizedString rightButtonName:nil leftButtonBlock:nil rightButtonBlock:nil];
            return;
        }
    
         TJQuickTradingNewController *tradVC = [TJQuickTradingNewController new];
          tradVC.model = cell.model;
          [weakself.navigationController pushViewController:tradVC animated:YES];
    }];

    
}


/**获取委托单*/
 - (void)loadNewData{
    _pageIndex = 1;
    //0.买单1.卖单
    NSString *direction = self.type == 0 ?@"BUY":@"SELL";
    
    NSDictionary *parameters = @{
        
        @"currency": self.coinName,
        @"size": @(10),
        @"payType": self.payType ?: @"",//支付方式 不传为全部订单收款方式,多种的话用-拼接(ALIAY,WECHAT,BANKCARD) 如 ALIAY-WECHAT
        @"page": @(_pageIndex),
        @"direction":direction,
    };
    
    weakify(self);
    [TJNetworking.shareNetworking POSTNew:@"otc/order/orderList" parameters:parameters success:^(NSDictionary * _Nonnull responseObject, NSDictionary *data) {
        
        weakself.displayEmpty = YES;
        
        [weakself.datas removeAllObjects];
        weakself.datas = [TJOrderListModel mj_objectArrayWithKeyValuesArray:data[@"data"]];
    
        [weakself.tableView reloadData];
        [weakself footerViewIsHidden:weakself.datas];
        [weakself.tableView.mj_header endRefreshing];
    } failure:^(NSError * _Nonnull error) {
        [weakself.tableView reloadData];
        [weakself.tableView.mj_header endRefreshing];
    }];
    
}

- (void)loadMoreData {
    _pageIndex++;
    
    //0.买单1.卖单
    NSString *direction = self.type == 0 ?@"BUY":@"SELL";
    
    NSMutableDictionary *parameters = @{
        
        @"currency": self.coinName,
        @"size": @(10),
        @"payType": self.payType ?: @"",//支付方式 不传为全部订单收款方式,多种的话用-拼接(ALIAY,WECHAT,BANKCARD) 如 ALIAY-WECHAT
        @"page": @(_pageIndex),
        @"direction":direction,
    }.mutableCopy;
    
    weakify(self);
    [TJNetworking.shareNetworking POSTNew:@"otc/order/orderList" parameters:parameters success:^(NSDictionary * _Nonnull responseObject, NSDictionary *data) {
        
        NSArray *arr = [TJOrderListModel mj_objectArrayWithKeyValuesArray:data[@"data"]];
        
        [weakself.datas addObjectsFromArray:arr];
         [weakself footerViewIsHidden:arr];
        [weakself.tableView reloadData];
        [weakself.tableView.mj_footer endRefreshing];
        
        
    } failure:^(NSError * _Nonnull error) {
        [weakself.tableView.mj_footer endRefreshing];
    }];
}


- (void)footerViewIsHidden:(NSArray *)arr {
    if (arr.count < 10) {
        self.tableView.mj_footer.hidden = YES;
    }else{
        self.tableView.mj_footer.hidden = NO;
    }
}
- (JXCategoryTitleView *)categoryView {
    if (_categoryView == nil) {
        _categoryView = [[JXCategoryTitleView alloc] init];
    }
    return _categoryView;
}

#pragma mark ---------------JXCategoryViewDelegate------------------
- (void)categoryView:(JXCategoryBaseView *)categoryView didSelectedItemAtIndex:(NSInteger)index {
    
    TJLog(@"index = %ld",index);
    
    self.coinName = self.categoryView.titles[index];
    [self loadNewData];

}

@end
